package com.nali.dal.statement.query;

import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.nali.dal.dao.general.IGeneralQueryDao;

/**
 * @author gavin Created on 2011-4-11
 */
public class SelectStatement {
	protected List<String> select;
	protected String from;
	protected List<String> where = Collections.emptyList();
	protected List<String> nativeWhere = Collections.emptyList();
	protected List<String> groupBy = Collections.emptyList();
	protected List<String> orderBy = Collections.emptyList();
	@Autowired
	protected ColumnUnitFactory columnUnitFactory;
	@Autowired
	private IGeneralQueryDao queryDao;
	private Selecter selecter;
	private DataDivider divider;
	
	public Selecter getSelecter() {
		return selecter;
	}
	
	@PostConstruct
	public void postConstruct() {
		InstantSelecter selecter;
		if(divider!=null) {
			selecter = new DivideSelecter();
			((DivideSelecter)selecter).setDivider(divider);
		} else {
			selecter = new InstantSelecter();
		}
		initInstantSelecter(selecter);
		this.selecter=selecter;
	}
	
	private void initInstantSelecter(InstantSelecter selecter) {
		SelectStatement statement = this;
		selecter.setSelect(statement .getSelect(), statement.getColumnUnitFactory());
		selecter.setFrom(statement.getFrom());
		selecter.setWhere(statement.getWhere(), statement.getColumnUnitFactory());
		selecter.setNativeWhere(statement.getNativeWhere());
		selecter.setGroupBy(statement.getGroupBy());
		selecter.setQueryDao(statement.getQueryDao());
		selecter.postConstruct();
		selecter.setOrderBy(orderBy);
	}

	public List<String> getSelect() {
		return select;
	}

	public void setSelect(List<String> select) {
		this.select = select;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public List<String> getWhere() {
		return where;
	}

	public void setWhere(List<String> where) {
		this.where = where;
	}

	public List<String> getGroupBy() {
		return groupBy;
	}

	public void setGroupBy(List<String> groupBy) {
		this.groupBy = groupBy;
	}

	public void setColumnUnitFactory(ColumnUnitFactory columnUnitFactory) {
		this.columnUnitFactory = columnUnitFactory;
	}

	public ColumnUnitFactory getColumnUnitFactory() {
		return columnUnitFactory;
	}

	public IGeneralQueryDao getQueryDao() {
		return queryDao;
	}

	public void setQueryDao(IGeneralQueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public List<String> getNativeWhere() {
		return nativeWhere;
	}

	public void setNativeWhere(List<String> nativeWhere) {
		this.nativeWhere = nativeWhere;
	}

	public void setDivider(DataDivider divider) {
		this.divider = divider;
	}

	public void setOrderBy(List<String> orderBy) {
		this.orderBy = orderBy;
	}

}
